/*
 * AddOperator.java
 *
 * Created on 23 Maret 2008, 23:14
 */
package usu.perpustakaan.buku.widget.admin;

import java.util.Calendar;
import java.util.Date;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import usu.perpustakaan.buku.data.DefaultOperator;
import usu.perpustakaan.buku.data.template.Operator;
import usu.perpustakaan.buku.util.StringUtil;
import usu.perpustakaan.buku.widget.template.PanelBlackGreen;
import usu.widget.ButtonGlass;
import usu.widget.text.DefaultDocument;

/**
 *
 * @author  usu
 */
public class EditOperator extends PanelBlackGreen {

    private static final long serialVersionUID = -1;
    private String invalidMessage;
    private Operator operator;
    private String id;

    /** Creates new form BeanForm */
    public EditOperator() {
        initComponents();
        initFinal();
    }

    /**
     * 
     * @return
     */
    public String getOperatorId() {
        return id;
    }

    /**
     * 
     * @return
     * @throws java.lang.IllegalArgumentException
     */
    public Operator getOperator() throws IllegalArgumentException {
        if (!isOperatorValid()) {
            throw new IllegalArgumentException("Operator Is Invalid");
        }
        DefaultOperator o = new DefaultOperator();
        o.setId(textId.getText());
        o.setAddress(textAddress.getText());
        o.setBorn((Date) textBorn.getValue());
        o.setContact(textContact.getText());
        o.setName(textName.getText());
        o.setPassword(getStringFromArrayChar(textPassword.getPassword()));
        return o;
    }

    /**
     * 
     * @param o 
     */
    public void setOperator(Operator o) {
        id = o.getId();
        operator = o;
        reset();
    }

    /**
     * 
     * @param c
     * @return
     */
    private String getStringFromArrayChar(char[] c) {
        return StringUtil.getStringFromArrayChar(c);
    }

    /**
     * is contain "
     * @param teks
     * @return
     */
    private boolean containSemiColomn(String teks) {
        return StringUtil.containSemiColomn(teks);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usu.perpustakaan.buku.widget.template.ViewPortGlass view = new usu.perpustakaan.buku.widget.template.ViewPortGlass();
        textAddress = new javax.swing.JTextArea();
        javax.swing.JLabel jLabel1 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel2 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel3 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel4 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel5 = new javax.swing.JLabel();
        javax.swing.JLabel jLabel6 = new javax.swing.JLabel();
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        buttonSubmit = new usu.widget.ButtonGlass();
        usu.widget.ButtonGlass buttonReset = new usu.widget.ButtonGlass();
        buttonCancel = new usu.widget.ButtonGlass();
        textId = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        textPassword = new usu.perpustakaan.buku.widget.template.PasswordBox();
        textName = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        textBorn = new usu.perpustakaan.buku.widget.template.FormatedTextBox();
        textContact = new usu.perpustakaan.buku.widget.template.TextBoxGlass();
        javax.swing.JLabel jLabel7 = new javax.swing.JLabel();

        view.setView(textAddress);

        textAddress.setColumns(20);
        textAddress.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        textAddress.setForeground(new java.awt.Color(255, 255, 255));
        textAddress.setLineWrap(true);
        textAddress.setRows(5);
        textAddress.setWrapStyleWord(true);
        textAddress.setBorder(javax.swing.BorderFactory.createEmptyBorder(10, 10, 10, 10));
        textAddress.setFocusAccelerator('A');
        textAddress.setOpaque(false);

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 3));
        setMinimumSize(new java.awt.Dimension(485, 339));
        setPreferredSize(new java.awt.Dimension(485, 339));

        jLabel1.setDisplayedMnemonic('I');
        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Id");

        jLabel2.setDisplayedMnemonic('P');
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Password");

        jLabel3.setDisplayedMnemonic('N');
        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Nama");

        jLabel4.setDisplayedMnemonic('L');
        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Lahir");

        jLabel5.setDisplayedMnemonic('K');
        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Kontak");

        jLabel6.setDisplayedMnemonic('A');
        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("Alamat");

        jScrollPane1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane1.setOpaque(false);
        jScrollPane1.setViewport(view);

        buttonSubmit.setMnemonic('U');
        buttonSubmit.setText("Ubah");
        buttonSubmit.setRoundRect(true);

        buttonReset.setMnemonic('R');
        buttonReset.setText("Reset");
        buttonReset.setRoundRect(true);
        buttonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetActionPerformed(evt);
            }
        });

        buttonCancel.setMnemonic('B');
        buttonCancel.setText("Batal");
        buttonCancel.setRoundRect(true);

        textId.setForeground(new java.awt.Color(255, 255, 255));
        textId.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textId.setFocusAccelerator('I');
        textId.setFont(new java.awt.Font("Tahoma", 1, 11));

        textPassword.setForeground(new java.awt.Color(255, 255, 255));
        textPassword.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textPassword.setFocusAccelerator('P');
        textPassword.setFont(new java.awt.Font("Tahoma", 1, 11));

        textName.setForeground(new java.awt.Color(255, 255, 255));
        textName.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textName.setFocusAccelerator('N');
        textName.setFont(new java.awt.Font("Tahoma", 1, 11));

        textBorn.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG))));
        textBorn.setForeground(new java.awt.Color(255, 255, 255));
        textBorn.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textBorn.setFocusAccelerator('L');
        textBorn.setFont(new java.awt.Font("Tahoma", 1, 11));
        textBorn.setValue(new java.util.Date());

        textContact.setForeground(new java.awt.Color(255, 255, 255));
        textContact.setHorizontalAlignment(javax.swing.JTextField.LEFT);
        textContact.setFocusAccelerator('K');
        textContact.setFont(new java.awt.Font("Tahoma", 1, 11));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Ubah Operator");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(textId, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(textPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(textName, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(textBorn, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
                            .addComponent(textContact, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(textPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(textName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(textBorn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(textContact, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSubmit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonCancel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonCancel() {
        return buttonCancel;
    }

    /**
     * 
     * @return
     */
    public ButtonGlass getButtonSubmit() {
        return buttonSubmit;
    }

    /**
     * reset all text component
     * @throws java.lang.IllegalArgumentException 
     */
    public void reset() throws IllegalArgumentException {
        if (operator == null) {
            throw new IllegalArgumentException();
        }
        textAddress.setText(operator.getAddress());
        textBorn.setValue(operator.getBorn());
        textContact.setText(operator.getContact());
        textId.setText(operator.getId());
        textName.setText(operator.getName());
        textPassword.setText(operator.getPassword());
    }

    /**
     * 
     * @return
     */
    public boolean isOperatorValid() {
        if (textId.getText().trim().equals("")) {
            invalidMessage = "ID Is Empty";
            return false;
        }
        if (containSemiColomn(textId.getText())) {
            invalidMessage = "ID Can't Contains Character \"";
            return false;
        }
        if (textPassword.getPassword().length < 1) {
            invalidMessage = "Password Is Empty";
            return false;
        }
        if (containSemiColomn(getStringFromArrayChar(textPassword.getPassword()))) {
            invalidMessage = "Password Can't Contains Character \"";
            return false;
        }
        if (textName.getText().trim().equals("")) {
            invalidMessage = "Name Is Empty";
            return false;
        }
        if (containSemiColomn(textName.getText())) {
            invalidMessage = "Name Can't Contains Character \"";
            return false;
        }
        if (containDigit(textName.getText())) {
            invalidMessage = "Name Can't Contains Digit Character";
            return false;
        }
        Date d = (Date) textBorn.getValue();
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        Calendar now = Calendar.getInstance();
        if (c.get(Calendar.YEAR) > now.get(Calendar.YEAR)) {
            invalidMessage = "Born Year Is Greater Than Now";
            return false;
        } else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
            if (c.get(Calendar.MONTH) > now.get(Calendar.MONTH)) {
                invalidMessage = "Born Month Is Greater Than Now";
                return false;
            } else if (c.get(Calendar.MONTH) == now.get(Calendar.MONTH)) {
                if (c.get(Calendar.DATE) > now.get(Calendar.DATE)) {
                    invalidMessage = "Born Date Is Greater Than Now";
                    return false;
                } else if (c.get(Calendar.DATE) == now.get(Calendar.DATE)) {
                    invalidMessage = "Born Date Is Now";
                    return false;
                }
            }
        }
        if (textContact.getText().trim().equals("")) {
            invalidMessage = "Contact Is Empty";
            return false;
        }
        if (containSemiColomn(textContact.getText())) {
            invalidMessage = "Contact Can't Contains Character \"";
            return false;
        }
        if (textAddress.getText().trim().equals("")) {
            invalidMessage = "Address Is Empty";
            return false;
        }
        if (containSemiColomn(textAddress.getText())) {
            invalidMessage = "Address Can't Contains Character \"";
            return false;
        }
        invalidMessage = null;
        return true;
    }

    /**
     * is contain digit
     * @param teks
     * @return
     */
    private boolean containDigit(String teks) {
        return StringUtil.containDigit(teks);
    }

    /**
     * 
     * @return
     */
    public String getInvalidMessage() {
        return invalidMessage;
    }

private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed
// TODO add your handling code here:
    reset();
}//GEN-LAST:event_buttonResetActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    usu.widget.ButtonGlass buttonCancel;
    usu.widget.ButtonGlass buttonSubmit;
    javax.swing.JTextArea textAddress;
    usu.perpustakaan.buku.widget.template.FormatedTextBox textBorn;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textContact;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textId;
    usu.perpustakaan.buku.widget.template.TextBoxGlass textName;
    usu.perpustakaan.buku.widget.template.PasswordBox textPassword;
    // End of variables declaration//GEN-END:variables

    /**
     * 
     */
    private void initFinal() {
        textName.setDocument(new DefaultDocument(64) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (containDigit(str)) {
                    return;
                }
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        textAddress.setDocument(new DefaultDocument() {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        textContact.setDocument(new DefaultDocument(64) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        textId.setDocument(new DefaultDocument(10) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
        textPassword.setDocument(new DefaultDocument(64) {

            private static final long serialVersionUID = -1;

            @Override
            public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
                if (containSemiColomn(str)) {
                    return;
                }
                super.insertString(offs, str, a);
            }
        });
    }
}
